package com.travel.server.manager.impl.command.order.data;

import com.travel.common.util.CollectionUtil;
import com.travel.common.util.CommonUtil;
import com.travel.common.util.DateUtil;
import com.travel.server.manager.dto.request.order.data.QueryOrderAnalysisListRequest;
import com.travel.server.manager.dto.request.project.TimeRequest;
import com.travel.server.manager.dto.response.order.data.QueryOrderAnalysisListResponse;
import com.travel.server.manager.dto.response.order.data.QueryOrderAnalysisListResponse.AnalysisItem;
import com.travel.server.manager.impl.command.AbstractCommand;
import com.travel.server.manager.impl.dal.OrderDal;
import com.travel.server.manager.impl.dal.ProjectDal;
import com.travel.server.manager.model.dto.data.OrderAnalysisDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: LiZhenhui
 * @Date: 2018/12/7 16:30
 * @Desc:
 */
@Component
@Slf4j
public class QueryOrderAnalysisListCommand extends AbstractCommand<QueryOrderAnalysisListRequest, QueryOrderAnalysisListResponse> {

    @Autowired
    private OrderDal orderDal;

    @Autowired
    private ProjectDal projectDal;

    @Override
    protected void checkInput(QueryOrderAnalysisListRequest request) {
        checkPageInput(request);
        checkTimeInput(request);
    }

    @Override
    public QueryOrderAnalysisListResponse handle(QueryOrderAnalysisListRequest request) {
        checkInput(request);

        QueryOrderAnalysisListResponse response = new QueryOrderAnalysisListResponse();
        response.setTotalNum(projectDal.selectUsefulTotal());
        response.setAnalysisList(makeAnalysisList(request));

        return response;
    }

    public List<AnalysisItem> makeAnalysisList(TimeRequest request){
        Date startTime = DateUtil.getDataTime(request.getStartTime());
        Date endTime = DateUtil.getDataEndTime(request.getEndTime());
        List<OrderAnalysisDto> list = orderDal.selectAnalysis(startTime, endTime, request.getOffset(), request.getLimit());

        List<AnalysisItem> analysisList = new ArrayList<>();
        if(!CollectionUtil.isEmpty(list)){
            for(OrderAnalysisDto dto : list){
                AnalysisItem item = new AnalysisItem();
                item.setComplete(dto.getCount() + "");
                item.setOrderCharge(dto.getChargeName());
                item.setPayAmount(CommonUtil.fenToYuan(dto.getAmount()).toString());
                item.setProject(dto.getProjectName());
                item.setVisit("0");
                item.setVisitPay("0%");
                analysisList.add(item);
            }
        }

        return analysisList;
    }
}
